package com.zhl.service_.impl;

import com.zhl.dao_.MessageMapper;
import com.zhl.entity.Message;
import com.zhl.service_.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/10/12.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;


    @Override
    public List<Message> messageList() {
        List<Message> messages = messageMapper.messageList();
        return messages;
    }

    @Override
    public List<Message> childMessageList(int id) {
        List<Message> messages = messageMapper.childMessageList(id);
        return messages;
    }

    @Override
    public int addMessage(Message message) {

        //如果是给其他留言进行回复,要对留言修改是否有回复的标识
        int writeBack = message.getWriteBack();
        if (writeBack != 0) {
            Message parentMessage = queryMessageById(writeBack);
            if (message.getHaschild() == 0) {
                parentMessage.setHaschild(1);
                messageMapper.updateHaschild(parentMessage);
            }
        }

        return messageMapper.addMessage(message);
    }

    @Override
    public Message queryMessageById(int id) {
        Message message = messageMapper.queryMessageById(id);
        return message;
    }

    //查询某文章下的留言
    @Override
    public List<Message> commentList(int belongArticle) {
        return messageMapper.commentList(belongArticle);
    }

    @Override
    public int deleteMessage(int id) {
        Message message = messageMapper.queryMessageById(id);
        //删除留言下的回复
        if (message.getHaschild() == 1) {
            messageMapper.deleteMessageByWriteBack(message.getId());
        }
        return messageMapper.deleteMessageById(id);
    }
}
